mod token;
use std::io;
use std::env;
use token::{tokenize, build_postfix_expression, calc_postfix};

// 控制台计算程序
fn console_calc() {
    // 获取控制台参数
    let args: Vec<String> = env::args().collect();
    // println!("{:?}", args);
    let show_detail = args.len() > 1 && args[1] == "--detail";
    let show_info   = args.len() > 1 && args[1] == "--info";
    if show_detail || show_info {
        println!("请在下一行输入式子:");
    }
    let mut input = String::new();
    io::stdin().read_line(&mut input).expect("式子错误！");
    let input = input.trim();
    // let input = "1 - (1.5 + 3.2) * -8.25";
    let tokens = tokenize(input);
    if show_detail {
        println!("tokens: {:?}", tokens);
    }
    let postfix = build_postfix_expression(tokens);
    if show_detail {
        println!("postfix: {:?}", postfix);
    }
    // 计算最终结果
    let res = calc_postfix(postfix);
    println!("{}", res);
}

fn main() {
    console_calc();
}